package org.example.forum.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.exception.ApplicationException;
import org.example.forum.model.Board;
import org.example.forum.services.IBoardService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
@Tag(name = "导航栏信息")
public class BoradControlle {
    @Resource
    private IBoardService boardService;
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;

    /**
     * 查询板块列表
     * @return
     */
    @GetMapping("/topList")
    @Operation(description = "板块信息" )
    public AppResult<List<Board>> topList(){
        log.info("首页板块个数为+"+indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if(boards==null){
            //如果查询信息为空,创建一个空列表返回给前端.
            boards=new ArrayList<>();
        }
        return AppResult.success(boards);

    }

    /**
     * 查询板块名字
     * @param id
     * @return
     */
    @GetMapping("/getById")
    @Operation(summary = "获取板块信息")
    public AppResult<Board> getById(@Parameter(description = "板块id") @RequestParam("id") @NonNull Long id){
        //调用数据库查询
        Board board = boardService.selectById(id);
        //对board进行校验
        if(board==null||board.getState()==1){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //返回结果
        return AppResult.success(board);


    }
}
